//
//  MyWIFIViewController.m
//  Router
//
//  Created by teki on 15/10/19.
//  Copyright © 2015年 dc. All rights reserved.
//

#import "MyWIFIListViewController.h"
#import "MyWIFITableViewCell.h"
#import "LoginResponseModel.h"
#import "DeviceManagerViewController.h"
#import "WIFIHelper.h"

@interface MyWIFIListViewController ()<UIAlertViewDelegate>
@property (nonatomic, strong) BindDeviceModel *bindDeviceModel;
@property (nonatomic,strong) WIFIHelper *wifiHelper;
@end

@implementation MyWIFIListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.wifiHelper = [[WIFIHelper alloc] init];
    
    self.title = @"我的小叶WIFI";
    self.tableView.tableFooterView = [UIView new];
    
    NSArray *devices = [UserSingleton shareInstance].userModel.bindDevice;
    self.contentList = [NSMutableArray arrayWithArray:devices];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.contentList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString * cellIdentifier = @"MyWIFITableViewCell";
    MyWIFITableViewCell *cell = (MyWIFITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[MyWIFITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.deviceModel = self.contentList[indexPath.row];
    return cell;
}



#pragma mark -
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.bindDeviceModel = self.contentList[indexPath.row];
    
    WIFIModel *wifi = [UserSingleton shareInstance].userModel.wifi;
    if (![wifi.device_id isEqualToString:self.bindDeviceModel.deviceID]) {
        NSString *message = [NSString stringWithFormat:@"确定切换到:%@",self.bindDeviceModel.ssid];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alertView show];
    }
}



#pragma mark -
#pragma mark   UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1)
    {
        [self switchDevice];
    }
}


#pragma mark -
#pragma mark   Request
- (void)switchDevice
{
    if (self.bindDeviceModel) {
        UserModel *user = [UserSingleton shareInstance].userModel.user[0];
        self.activity = [self startActivityWithTitle:@"正在切换..."];
        WEAKSELF
        [self.wifiHelper refreshDataWithDeviceID:self.bindDeviceModel.deviceID userName:user.userName success:^(BOOL succ, id responseObject, NSString *msg) {
            __strong __typeof(weakSelf)strongSelf = weakSelf;
            if (responseObject)
            {
                [strongSelf stopActivityWithActivity:strongSelf.activity title:@"切换成功!"];
                [strongSelf refreshDataWithResponse:responseObject];
            }
            else
            {
                [strongSelf stopActivityWithActivity:strongSelf.activity title:@"切换失败!"];
            }
        } failure:^(NSError *error) {
            NSString *failedReason = [error localizedDescription];
            [self stopActivityWithActivity:self.activity title:failedReason];
        }];
    }
}



- (void)refreshDataWithResponse:(NSDictionary *)dict
{
    if (dict)
    {
        NSArray *viewController = [self.tabBarController viewControllers];
        BaseNavigationViewController *baseNAVI = [viewController objectAtIndex:2];
        DeviceManagerViewController *VC = [[baseNAVI viewControllers] firstObject];
        [VC clearData];
        
        LoginResponseModel *responseModel = [LoginResponseModel objectWithKeyValues:dict];
        
        self.contentList = [NSMutableArray arrayWithArray:responseModel.bindDevice];
        [self.tableView reloadData];
        
        [UserSingleton shareInstance].userModel.bindDevice = responseModel.bindDevice;
        [UserSingleton shareInstance].userModel.wifi = responseModel.wifi;
        [UserSingleton shareInstance].userModel.rate = responseModel.rate;
        NSArray *array = [responseModel.stations sortedArrayUsingComparator:^NSComparisonResult(StationModel *  _Nonnull obj1, StationModel*  _Nonnull obj2) {
            NSNumber *number1 = @(obj1.master);
            NSNumber *number2 = @(obj2.master);
            return [number2 compare:number1];
        }];
        [UserSingleton shareInstance].userModel.stations = array;
        [[UserSingleton shareInstance] saveData];
    }
}
@end
